<html lang="zh_CN">
  <head>
    <title>Ocean Pay</title>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=none"
    />
    <title>OceanPay</title>
    <script
      type="text/javascript"
      src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"
    ></script>
  </head>

  <body>
    <div>
      Loading
    </div>
    <script>
      const query = window.location.href.split('?')[1]

      const queryObj = query.split('&').reduce((prev, current) => {
        const [key, value] = current.split('=')
        prev[key] = value
        return prev
      }, {})

      var BASE_URL = decodeURIComponent(queryObj.url)
      
      var serializer = obj => {
        if (!obj) return ''

        return Object.entries(obj)
          .reduce((prev, [key, value]) => {
            prev.push(key + '=' + value)
            return prev
          }, [])
          .join('&')
      }

      function oceanPayCharacterEscape(params) {
        return params
          .toString()
          .trim()
          .replace(/\</, '&lt;')
          .replace(/\>/, '&gt;')
          .replace(/\'/, '&#039;')
          .replace(/\\/, '&quot;')
      }

      function simpleAjax({
        url,
        method,
        data,
        token,
        successFn,
        failFn = function() {}
      }) {
        const xhr = new XMLHttpRequest()
        if (method.toUpperCase() === 'GET') {
          data = serializer(data)
          url = BASE_URL + url + '?' + data
        } else {
          url = BASE_URL + url
        }
		
		console.log(url)

        xhr.open(method.toUpperCase(), url, true)

        xhr.setRequestHeader('Authorization', token)

        xhr.onreadystatechange = function() {
          if (xhr.readyState === XMLHttpRequest.DONE) {
            const status = xhr.status

            if (status === 0 || (status >= 200 && status < 400)) {
              const response = JSON.parse(xhr.response)
              if (response.code === 200) {
                successFn(response)
              } else {
                failFn(response)
              }
            }
          }
        }

        if (method.toUpperCase() === 'POST') {
          xhr.send(JSON.stringify(data))
        } else {
          xhr.send(null)
        }
      }

      const handleResponse = (data) => {
				const cl = document.getElementById('console')
				
				const response = JSON.parse(data.data)
				
				const formAttr = [
					['id', 'submit-form'],
					['action', response.apiUrl],
					['method', 'POST'],
					['style', 'display: none'],
					['content-type', 'multipart/form-data']
				]
				const form = document.createElement('form')
			
				formAttr.forEach(([key, value]) => {
					form.setAttribute(key, value)
				})
			
				const createInput = attrs => {
					const input = document.createElement('input')
			
					Object.entries(attrs).forEach(([key, value]) => {
						input.setAttribute(key, oceanPayCharacterEscape(value || ''))
					})
			
					return input
				}
			
				const fragment = document.createDocumentFragment()
	
				const inputAtt = Object.keys(response).map(val => ({
					key: val,
					val: response[val]
				}))
			
				inputAtt.forEach(current => {
					const input = createInput({
						name: current.key,
						value: current.val
					})
					fragment.appendChild(input)
				})
			
				form.appendChild(fragment)
			
				const button = document.createElement('button')
				button.type = 'submit'
				
				form.appendChild(button)
			
				document.body.appendChild(form)
			
				form.submit()
			}
      
      // #ifdef APP-PLUS
      simpleAjax({
        url: '/oceanPay/getPayInfo',
        method: 'GET',
        data: {
          isApp: 1,
          orderId: queryObj.order_id,
          url: 'https://www.wharfon.com/',
          jumpUrl: 'jump.html'
        },
        token: queryObj.token,
        successFn: function(res) {
          handleResponse(res)
        },
      
        failFn: function() {
        }
      })

      // #endif
    </script>
  </body>
</html>